package com.apartmentManage.service.impl;

import com.apartmentManage.Dao.RoomDao;
import com.apartmentManage.entity.Room;
import com.apartmentManage.service.RoomService;
import com.apartmentManage.until.IntUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomDao roomDao;

    /**
     *
     * @param height 层数
     * @param perNumber 每间房间数
     * @param headNo 第一个房间号
     * @param intoNumber 每间宿舍居住人数
     * @param apartmentNo 楼栋号
     * @return
     */
    @Override
    public boolean saveList(Integer height, Integer perNumber, Integer headNo, Integer intoNumber, Integer apartmentNo) {
        if (height==null || height==0 || perNumber==null || perNumber==0)
            return false;
        if (getByApartmentNo(apartmentNo).size()>0)
            return false;
        List<Room> list=new ArrayList<>();
//        获取headNo长度
        Integer length = IntUntil.LengthNum(headNo);
        headNo = headNo + (int) Math.pow(10, length) * apartmentNo;
       System.out.println(headNo);
        for (int i=1;i<=height;i++){
//            模拟
            Integer headNoVo=headNo;
            for (int j=1;j<=perNumber;j++){
                Room room=new Room();
                room.setRoomno(headNoVo++);
                room.setBednumber(intoNumber);
                room.setApartmentno(apartmentNo);
                list.add(room);
            }
//            10的length-1的平方根
              headNo+=(int)Math.pow(10, length - 1);
        }
        return roomDao.saveList(list)>0;
    }

    @Override
    public List<Room> getAll() {
        return roomDao.getAll();
    }

    @Override
    public List<Room> getByApartmentNo(Integer apartmentNo) {
        return roomDao.getByApartmentNo(apartmentNo);
    }
}
